#include "BranchingPrimaryGenerator.hh"

BranchingPrimaryGenerator::
	BranchingPrimaryGenerator()
{
	// do nothing
}

BranchingPrimaryGenerator::
	~BranchingPrimaryGenerator()
{
	//do nothing
}

void BranchingPrimaryGenerator::
	addPrimaryGenerator(G4VPrimaryGenerator* generator)
{
	//this->addPrimaryGenerator(generator, 1.0);
}

void BranchingPrimaryGenerator::
	addPrimaryGenerator(G4VPrimaryGenerator* generator, G4double fractionalIntensity)
{
	generatorList.push_back(generator);
	probabilityList.push_back(fractionalIntensity);
}

void BranchingPrimaryGenerator::
	GeneratePrimaryVertex(G4Event* evt)
{
	
	G4double cdfProb = 0;
	G4double random = G4UniformRand();
	random = G4UniformRand();
	vector<G4double>::iterator prob_it = probabilityList.begin();
	vector<G4VPrimaryGenerator*>::iterator gen_it = generatorList.begin();
	

	int i = 0;
	while(*prob_it != probabilityList.back())
	{
		cdfProb += *prob_it;
		if(random <= cdfProb)
		{
			break;
		}

		prob_it++;
		gen_it++;
		i++;
	}

	//G4cout << "BranchingPrimaryGenerator generating particles \n";
	(*gen_it)->GeneratePrimaryVertex(evt);
	
	
}

